<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%rbac}}`.
 */
class m200507_070821_create_rbac_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        Yii::$app->getDb()->createCommand("
drop table if exists `admin_auth_assignment`;
drop table if exists `admin_auth_item_child`;
drop table if exists `admin_auth_item`;
drop table if exists `admin_auth_rule`;

create table `admin_auth_rule`
(
   `name`                 varchar(64) not null,
   `data`                 blob,
   `created_at`           integer,
   `updated_at`           integer,
    primary key (`name`)
) engine InnoDB;

create table `admin_auth_item`
(
   `name`                 varchar(64) not null,
   `type`                 smallint not null,
   `description`          text,
   `rule_name`            varchar(64),
   `data`                 blob,
   `created_at`           integer,
   `updated_at`           integer,
   primary key (`name`),
   foreign key (`rule_name`) references `admin_auth_rule` (`name`) on delete set null on update cascade,
   key `type` (`type`)
) engine InnoDB;

create table `admin_auth_item_child`
(
   `parent`               varchar(64) not null,
   `child`                varchar(64) not null,
   primary key (`parent`, `child`),
   foreign key (`parent`) references `admin_auth_item` (`name`) on delete cascade on update cascade,
   foreign key (`child`) references `admin_auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;

create table `admin_auth_assignment`
(
   `item_name`            varchar(64) not null,
   `user_id`              varchar(64) not null,
   `created_at`           integer,
   primary key (`item_name`, `user_id`),
   foreign key (`item_name`) references `admin_auth_item` (`name`) on delete cascade on update cascade,
   key `admin_auth_assignment_user_id_idx` (`user_id`)
) engine InnoDB;

        
        ")->execute();
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable('{{%rbac}}');
    }
}
